package com.ruoyi.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.ParkingInfo;
import com.ruoyi.domain.ParkingRecordGate;
import com.ruoyi.mapper.ParkingInfoMapper;
import com.ruoyi.mapper.ParkingRecordGateMapper;
import com.ruoyi.service.IParkingRecordGateService;
import com.ruoyi.wss.WebSocketMessageService;
import com.ruoyi.wss.dahua.Enum.MasWebsocketEnum;
import com.ruoyi.wss.dahua.request.CommonRequest;
import com.ruoyi.wss.dahua.request.OpenGateRequest;
import com.ruoyi.wss.dahua.request.RequestData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 远程开闸记录Service业务层处理
 * 
 * @author dongdong
 * @date 2025-08-30
 */
@Slf4j
@Service
public class ParkingRecordGateServiceImpl implements IParkingRecordGateService 
{
    @Resource
    private ParkingRecordGateMapper parkingRecordGateMapper;
    @Resource
    private ParkingInfoMapper parkingInfoMapper;
    @Autowired
    private WebSocketMessageService webSocketMessageService;


    /**
     * 查询远程开闸记录
     * 
     * @param id 远程开闸记录主键
     * @return 远程开闸记录
     */
    @Override
    public ParkingRecordGate selectParkingRecordGateById(Long id)
    {
        return parkingRecordGateMapper.selectParkingRecordGateById(id);
    }

    /**
     * 查询远程开闸记录列表
     * 
     * @param parkingRecordGate 远程开闸记录
     * @return 远程开闸记录
     */
    @Override
    @DataScope(deptAlias = "prg")
    public List<ParkingRecordGate> selectParkingRecordGateList(ParkingRecordGate parkingRecordGate)
    {
        return parkingRecordGateMapper.selectParkingRecordGateList(parkingRecordGate);
    }

    /**
     * 新增远程开闸记录
     * 
     * @param parkingRecordGate 远程开闸记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertParkingRecordGate(ParkingRecordGate parkingRecordGate) {
        ParkingInfo parkingInfo = parkingInfoMapper.selectParkingInfoById(parkingRecordGate.getParkId());
        Optional.ofNullable(parkingInfo).orElseThrow(() -> new RuntimeException("无效的请求信息"));
        String userName = getUsername();
        // 组装请求参数
        OpenGateRequest openGateRequest = new OpenGateRequest();
        openGateRequest.setPlatformId(parkingInfo.getPlatformId());
        openGateRequest.setParkingLotCode(parkingInfo.getParkCode());
        openGateRequest.setLaneCode(parkingRecordGate.getLaneCode());
        openGateRequest.setCarNum(parkingRecordGate.getCarNum());
        openGateRequest.setOperatorName(userName);
        Boolean result = sendOpenGateRequest(openGateRequest);
        if(result){
            parkingRecordGate.setCreateTime(DateUtils.getNowDate());
            parkingRecordGate.setCreateBy(userName);
            parkingRecordGate.setDeptId(parkingInfo.getDeptId());
            return parkingRecordGateMapper.insertParkingRecordGate(parkingRecordGate);
        }
        return 0;
    }

    /**
     * 修改远程开闸记录
     * 
     * @param parkingRecordGate 远程开闸记录
     * @return 结果
     */
    @Override
    public int updateParkingRecordGate(ParkingRecordGate parkingRecordGate)
    {
        parkingRecordGate.setUpdateTime(DateUtils.getNowDate());
        return parkingRecordGateMapper.updateParkingRecordGate(parkingRecordGate);
    }

    /**
     * 批量删除远程开闸记录
     * 
     * @param ids 需要删除的远程开闸记录主键
     * @return 结果
     */
    @Override
    public int deleteParkingRecordGateByIds(Long[] ids)
    {
        return parkingRecordGateMapper.deleteParkingRecordGateByIds(ids);
    }

    /**
     * 删除远程开闸记录信息
     * 
     * @param id 远程开闸记录主键
     * @return 结果
     */
    @Override
    public int deleteParkingRecordGateById(Long id)
    {
        return parkingRecordGateMapper.deleteParkingRecordGateById(id);
    }

    /**
     * 请求下级平台远程开闸
     * @param openGateRequest
     * @return
     */
    public Boolean sendOpenGateRequest(OpenGateRequest openGateRequest){
        RequestData<OpenGateRequest> requestData = new RequestData<>(
                MasWebsocketEnum.REMOTE_OPEN_GATE.getMethod(), openGateRequest);
        CommonRequest<OpenGateRequest> openRequest = new CommonRequest<>("transform", 1, requestData);
        log.info("远程开闸请求JSON：{}", openGateRequest);
        try {
            Boolean result = webSocketMessageService.operationDevice(openGateRequest.getPlatformId(), openRequest);
            log.info("远程开闸结果: {}", result);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("未识别到出场车牌号");
        }
    }

}
